﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.paypal.sdk.services;
using com.paypal.sdk.profiles;
using com.paypal.sdk.util;

namespace DigitalGoodsExample
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NVPCallerServices caller = new NVPCallerServices();
            IAPIProfile profile = ProfileFactory.createSignatureAPIProfile();
            /*WARNING: Do not embed plaintext credentials in your application code.
            Doing so is insecure and against best practices.Your API credentials must 
            be handled securely. Please considerencrypting them for use in any production 
            environment, and ensurethat only authorized individuals may view or modify them.*/

            // Set up your API credentials, PayPal end point, API operation and version.profile.
            profile.APIUsername = Constants.API_USERNAME;
            profile.APIPassword = Constants.API_PASSWORD;
            profile.APISignature = Constants.API_SIGNATURE;
            profile.Environment = Constants.ENVIRONMENT;

            caller.APIProfile = profile;
            NVPCodec encoder = new NVPCodec(); 
            encoder["VERSION"] = "65.1";
            encoder["METHOD"] = "SetExpressCheckout";

            // Add request-specific fields to the request.
            encoder["RETURNURL"] = "return.aspx";
            encoder["CANCELURL"] = "Default.aspx";
            encoder["PAYMENTREQUEST_0_CURRENCYCODE"] = "USD";
            encoder["PAYMENTREQUEST_0_PAYMENTACTION"] = "SALE";
            encoder["PAYMENTREQUEST_0_AMT"] = "300.00";
            encoder["PAYMENTREQUEST_0_ITEMAMT"] = "200.00";
            encoder["PAYMENTREQUEST_0_TAXAMT"] = "100.00";
            encoder["PAYMENTREQUEST_0_DESC"] = "Movies";
            encoder["L_PAYMENTREQUEST_0_ITEMCATEGORY0"] = "Digital";
            encoder["L_PAYMENTREQUEST_0_ITEMCATEGORY1"] = "Digital";
            encoder["L_PAYMENTREQUEST_0_NAME0"] = "Movie 1";
            encoder["L_PAYMENTREQUEST_0_NAME1"] = "Movie 2";
            encoder["L_PAYMENTREQUEST_0_NUMBER0"] = "10101";
            encoder["L_PAYMENTREQUEST_0_NUMBER1"] = "20202";
            encoder["L_PAYMENTREQUEST_0_QTY0"] = "1";
            encoder["L_PAYMENTREQUEST_0_QTY1"] = "1";
            encoder["L_PAYMENTREQUEST_0_TAXAMT0"] = "50.00";
            encoder["L_PAYMENTREQUEST_0_TAXAMT1"] = "50.00";
            encoder["L_PAYMENTREQUEST_0_AMT0"] = "100.00";
            encoder["L_PAYMENTREQUEST_0_AMT1"] = "100.00";
            encoder["L_PAYMENTREQUEST_0_DESC0"] = "Download";
            encoder["L_PAYMENTREQUEST_0_DESC1"] = "Download";

            // Execute the API operation and obtain the response.

            string pStrrequestforNvp = encoder.Encode();
            string pStresponsenvp = caller.Call(pStrrequestforNvp);
            NVPCodec decoder = new NVPCodec(); decoder.Decode(pStresponsenvp);
            string Token = decoder["TOKEN"];
            Response.Write(
                "<br /><a href='https://www.sandbox.paypal.com/incontext?useraction=continue&token=" + 
                Token + "' id='submitBtn'><img src='https://www.paypal.com/en_US/i/btn/btn_dg_pay_w_paypal.gif' /></a>");
        }
    }
}